<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
use Think\Model;

class ManagerController extends BaseController {
    function admlist(){
    	$ADM = new Model('Admin');
        $totalRows = $ADM->count();
    	$page = new \Think\Page($totalRows, 10);
    	$admlist = $ADM->limit($page->firstRow,$page->listRows)->order('id desc')->select();
        $show = $page->show();
        $this->assign('page',$show);
        $this->assign('admlist',$admlist);
    	$this->display('manager-admlist');
    }
    
    function delete() {
        $id = I('id','');
        if($id == '' || $id == 1){
            $this->error('参数错误');
        } else {
            $ADM = new Model('Admin');
            $rs = $ADM->where(['id'=>$id])->delete();
            if($rs !== FALSE){
                $this->success('删除成功');
            } else {
                $this->error($ADM->getDbError());
            }
        }
    }
    
    function insert() {
        if(IS_POST){
            $salt = rand_string(8);
            $data = [
                'uuid' => makeUuid(),
                'username' => I('username'),
                'password' => md5(I('password').$salt),
                'salt' => $salt,
                'email' => I('email'),
                'mobile' => I('mobile'),
                'status' => I('status'),
            ];
            $ADM = D('Admin');
            if(!$ADM->create($data)){
                $this->error($ADM->getError());
            } else {
                $rs = $ADM->add();
                if($rs !== FALSE){
                    $this->success('增加成功');
                } else {
                    $this->error('增加失败');
                }
            }
        }else{
            $this->display('manager-insert');
        }
    }
    
    function edit() {
        if(IS_POST){
            $id = I('id','');
            if($id != ''){
                $map['id'] = $id;
                if(I('username') != ''){
                    $map['username'] = I('username');
                }
                if(I('password') != ''){
                    $salt = rand_string(8);
                    $map['password'] = md5(I('password').$salt);
                    $map['salt'] = $salt;
                }
                if(I('email') != ''){
                    $map['email'] = I('email');
                }
                if(I('mobile') != ''){
                    $map['mobile'] = I('mobile');
                }
                $map['status'] = I('status');
                $ADM = D('Admin');
                if(!$ADM->create($map)){
                    $this->error($ADM->getError());
                } else {
                    $rs = $ADM->save();
                    if($rs !== FALSE){
                        $this->success('编辑成功');
                    } else {
                        $this->error('编辑失败');
                    }
                }
            } else {
                $this->error('参数错误');
            }
        } else {
            $id = I('id','');
            if($id != ''){
                $ADM = new Model('Admin');
                $edit = $ADM->where(['id'=>$id])->find();
                if($edit){
                    $this->assign('edit',$edit);
                    $this->display('manager-edit');
                } else {
                    $this->error('管理员不存在');
                }
            }else{
                $this->error('参数错误');
            }
        }
    }

    function status(){
        $id = I('id','');
        $op = I('op') ? 1 : 0;
        if($id != ''){
            $ADM = new Model('Admin');
            $rs = $ADM->where(['id'=>$id])->setField('status',$op);
            if($rs !== false){
                $this->success('操作成功');
            }else{
                $this->error('操作失败，稍后再试');
            }
        }else{
            $this->error('参数错误');
        }
    }
    
}
